/* 
 *  <copyright file="ScreenManager.cs" company="Wesley Jowitt">
 *  
 *  SobrietyEngine: Copyright (c) 2011 Wesley Jowitt
 *  
 *  This software is distributed under a software license.
 *   * As of the creation time of this file, the license in use is the Microsoft Permissive License (Ms-PL) 
 *    * The license can be viewed at:
 *     * License.Txt in the root folder of this project
 *      * http://sobrietyengine.codeplex.com/license
 *       * http://www.microsoft.com/opensource/licenses.mspx
 *       
 *  </copyright>
 *   */

using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

using SobrietyEngine.Assets;

namespace SobrietyEngine.Screens
{
    public class ScreenManager : DrawableGameComponent, SobrietyEngine.Screens.IScreenManager
    {

        List<IScreen> screens = new List<IScreen>();
        List<IScreen> screensToUpdate = new List<IScreen>();

        public Boolean AutoDraw { get; set; }
        //InputState input = new InputState();
        //InputGameService inputGameService = null;
        //IEventRouter EventRouter = null;

        IGraphicsDeviceService _graphicsDeviceService;

        
        SpriteBatch spriteBatch;
        SpriteFont diagnosticSpriteFont;
        SpriteFont menuSpriteFont;

        Viewport fullScreenViewport;

        public IAssetGameService AssetGameService { get; private set; }


        public Viewport FullScreenViewport
        {
            get { return fullScreenViewport; }
            set { fullScreenViewport = value; }
        }

        new public Game Game
        {
            get { return base.Game; }
        }

        new public GraphicsDevice GraphicsDevice
        {
            get { return base.GraphicsDevice; }
        }

        public SpriteBatch SpriteBatch
        {
            get { return spriteBatch; }
        }

        public SpriteFont DiagnosticSpriteFont
        {
            get { return diagnosticSpriteFont; }
        }


        public SpriteFont MenuSpriteFont
        {
            get { return menuSpriteFont; }
        }

        public Vector2 ScreenCenter
        {
            get { return new Vector2(_graphicsDeviceService.GraphicsDevice.Viewport.Width / 2, _graphicsDeviceService.GraphicsDevice.Viewport.Height / 2); }
        }

        public int ScreenWidth
        {
            get { return _graphicsDeviceService.GraphicsDevice.Viewport.Width; }
        }

        public int ScreenHeight
        {
            get { return _graphicsDeviceService.GraphicsDevice.Viewport.Height; }
        }

        public ScreenManager(Game game)
            : base(game)
        {
            Game.Services.AddService(typeof(ScreenManager), this);
            //contentManager = new ContentManager(gameServices.Services);            

            _graphicsDeviceService = (IGraphicsDeviceService)game.Services.GetService(
                                                        typeof(IGraphicsDeviceService));
            if (_graphicsDeviceService == null)
                throw new InvalidOperationException("No graphics device service.");

            AutoDraw = true;
        }

        public override void Initialize()
        {
            AssetGameService = (IAssetGameService) Game.Services.GetService(typeof(IAssetGameService));
            base.Initialize();
        }

        protected override void LoadContent()
        {
            // Load content belonging to the screen manager.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            diagnosticSpriteFont = AssetGameService.Load<SpriteFont>("Fonts/diagnosticFont");
            menuSpriteFont = AssetGameService.Load<SpriteFont>("Fonts/menuFont");
        }

        protected override void UnloadContent()
        {
        }

        public override void Update(GameTime gameTime)
        {

            screensToUpdate = new List<IScreen>(screens);
            IScreen screen;
            // Make a copy of the master screen list, to avoid confusion if
            // the process of updating one screen adds or removes others.

            bool coveredByOtherScreen = false;
            IScreen prevScreen = null;

            // Loop as long as there are screens waiting to be updated.
            for(int screenNum = screensToUpdate.Count - 1; screenNum >= 0 ; --screenNum)
            {
                screen = (IScreen)screensToUpdate[screenNum];                               

                // Update the screen.
                if(screen.Overlay)
                    screen.Update(gameTime, false, null);
                else
                    screen.Update(gameTime, coveredByOtherScreen, prevScreen);

                if (screen.MarkedForRemoval)
                    RemoveScreen(screen);

                if (!screen.Popup && !screen.MarkedForRemoval)
                {
                    prevScreen = screen;
                    coveredByOtherScreen = true;
                }
            }
        }

        public override void Draw(GameTime gameTime)
        {
            for (int i = screens.Count - 1; i >= 0; --i)
            {
                if (screens[i].Visible)
                {
                    screens[i].Draw(gameTime);
                }
            }
        }


        public void AddScreen(IScreen screen)
        {
            screen.ScreenManager = this;
            screen.Initialize();
            screens.Add(screen);            
        }

        public void RemoveScreen(IScreen screen)
        {
            screen.Remove();
            screens.Remove(screen);
        }

        public void RemoveAllBut(IScreen screen)
        {
            for (int i = 0; i < screens.Count; ++i)
                screens[i].Remove();
            screens.Clear();
            screens.Add(screen);
        }

        public void RemoveAllScreens()
        {
            for (int i = 0; i < screens.Count; ++i)
                screens[i].Remove();
            screens.Clear();
        }


        public IScreen[] GetScreens()
        {
            return screens.ToArray();
        }

    }
}
